//
//  KFTools.m
//  Patient
//
//  Created by lbh on 2021/2/8.
//  Copyright © 2021年 lbh. All rights reserved.
//

#import "KFTools.h"

@import CoreTelephony;
@implementation KFTools

+ (NSString *)getSignString:(NSDictionary *)paramters {
    if (paramters == nil) {
        return @"";
    }
    
    NSMutableString *appendParam = nil;
    NSArray *keys = [paramters.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    for(NSString *key in keys)
    {
        if(!appendParam)
            appendParam = [[NSMutableString alloc] init];
        [appendParam appendFormat:@"%@=%@%@",key , [NSString escapedString:[paramters objectForKey:key]],[key isEqualToString:keys.lastObject]?@"":@"&"];
        
    }
    
    return  [appendParam copy];
    
}
+ (NSError *)makeError:(NSString *)message code:(NSInteger)code{
    return  [[NSError alloc] initWithDomain:@"200" code:code userInfo:@{NSLocalizedDescriptionKey:message}];
}
+ (NSString *)decodeError:(NSError *)error{
    
    NSString *message = error.userInfo[@"NSLocalizedDescription"];
    return message;
}

+ (BOOL)isFirstAppLaunching {
    NSString *systemVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    NSString *runversionNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"appVersion"];
    
    if ([NSString isBlankString:runversionNumber] || ![systemVersion isEqualToString:runversionNumber]) {
        return YES;
    }
    return NO;
}
///保存APP版本号
+ (void)saveAppVersion {
    NSString *systemVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    [[NSUserDefaults standardUserDefaults] setObject:systemVersion forKey:@"appVersion"];
}
///查询APP版本号
+ (NSString *)appVersion{
    NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
    NSString* version =[infoDict objectForKey:@"CFBundleShortVersionString"];
    return version;
}

/// 新旧版本号对比是否需要升级:YES:需要 NO:不需要
/// @param versionString 获取的版本号
/// @param myVerson 本地旧版本号
+ (BOOL)comparisonVersion:(NSString *)versionString localVersion:(NSString *)myVerson {
    
    if (versionString.length <= 0) {
        
        return NO;
    }
    NSArray *versionArray = [versionString componentsSeparatedByString:@"."];
    if (versionArray.count < 3) {
        
        return NO;
    }
    BOOL showStrongUpdateApp = NO;
    NSInteger version1 = 0;
    NSInteger version2 = 0;
    NSInteger version3 = 0;
    version1 = [[versionArray objectAtIndex:0] integerValue];
    version2 = [[versionArray objectAtIndex:1] integerValue];
    version3 = [[versionArray objectAtIndex:2] integerValue];
    
    NSArray *myVersionArray = [myVerson componentsSeparatedByString:@"."];
    if (myVersionArray.count < 3) {
        
        return NO;
    }
    NSInteger myVersion1 = 0;
    NSInteger myVersion2 = 0;
    NSInteger myVersion3 = 0;
    myVersion1 = [[myVersionArray objectAtIndex:0] integerValue];
    myVersion2 = [[myVersionArray objectAtIndex:1] integerValue];
    myVersion3 = [[myVersionArray objectAtIndex:2] integerValue];
    if (version1 > myVersion1) {
        
        showStrongUpdateApp = YES;
    }
    else if (version1 == myVersion1 && version2 > myVersion2) {
        
        showStrongUpdateApp = YES;
    }
    else if (version1 == myVersion1 && version2 == myVersion2 && version3 > myVersion3) {
        
        showStrongUpdateApp = YES;
    }
    return showStrongUpdateApp;
}
///获取当前VC
+ (UIViewController *)getCurrentVC{
    UIViewController *result = nil;
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
   
 //app默认windowLevel是UIWindowLevelNormal，如果不是，找到UIWindowLevelNormal的
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    id  nextResponder = nil;
    UIViewController *appRootVC=window.rootViewController;
    appRootVC = [self topPresentedViewController:appRootVC];
    nextResponder = appRootVC;
    if(![nextResponder isKindOfClass:UIViewController.class]){
        return nil;
    }
    else{
        result = [self getTopWithViewController:nextResponder];
    }
    return result;
}

+ (UIViewController *)topPresentedViewController:(UIViewController *)viewController{
    UIViewController *presentedViewController = viewController.presentedViewController;
    if(presentedViewController == nil){
        return viewController;
    }
    return [self topPresentedViewController:presentedViewController];
}
+ (UIViewController *)getTopWithViewController:(UIViewController *)viewController{
    if([viewController isKindOfClass:[UITabBarController class]]){
        UITabBarController *tab = (UITabBarController *)viewController;
        if(tab.viewControllers){
            UIViewController *vc = [self getTopWithViewController:tab.viewControllers[tab.selectedIndex]];
            return vc;
        }
    }
    else if([viewController isKindOfClass:[UINavigationController class]]){
        UINavigationController *nav = (UINavigationController *)viewController;
        if(nav.viewControllers){
            UIViewController *vc = [self getTopWithViewController:nav.viewControllers.lastObject];
            return vc;
        }
    }
    return viewController;
}
///跳到App Store指定APP
+ (void)pushAppStoreLink:(NSString *)appID{
    
    NSString *appLinke = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appID];
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:appLinke]];
}
@end
